﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using DD.UI.MediaCenter.ObjectModels;

namespace DD.UI.MediaCenter
{
    public class Utils
    {
        private static string DownloadPlaylist(string fileName)
        {
            string name;
            var request = (HttpWebRequest) WebRequest.Create(fileName);
            using (var response = (HttpWebResponse) request.GetResponse())
            {
                using (var stream = response.GetResponseStream())
                {
                    using (var stream2 = new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate))
                    {
                        int count = -1;
                        long num2 = 0x200L;
                        byte[] buffer = new byte[num2];
                        do
                        {
                            count = stream.Read(buffer, 0, buffer.Length);
                            stream2.Write(buffer, 0, count);
                        }
                        while (count != 0);
                        name = stream2.Name;
                    }
                }
            }
            return name;
        }

        public static Station[] GetStationsFrom_m3u(Uri playlistUri)
        {
            string localPath;
            if (!playlistUri.IsFile && (playlistUri.HostNameType == UriHostNameType.Dns))
            {
                localPath = DownloadPlaylist(playlistUri.OriginalString);
            }
            else
            {
                localPath = playlistUri.LocalPath;
            }
            return GetStationsFromLocalFile_m3u(localPath);
        }

        public static Station[] GetStationsFromLocalFile_m3u(string fileName)
        {
            using (var reader = File.OpenText(fileName))
            {
                string[] strArray = reader.ReadToEnd().Split(new string[] { "\n", "\r", "\n\r", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                var list = new List<Station>();
                if (strArray[0].Trim().ToUpper() == "#EXTM3U")
                {
                    string serviceName = string.Empty;
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (strArray[i].StartsWith("#EXTINF", StringComparison.CurrentCultureIgnoreCase))
                        {
                            string[] strArray2 = strArray[i].Split(new string[] { ":", "," }, StringSplitOptions.None);
                            if (strArray2.Length > 2)
                            {
                                serviceName = strArray2[2];
                            }
                            list.Add(new Station(serviceName, strArray[++i]));
                        }
                        else if (strArray[i].StartsWith("# ", StringComparison.CurrentCultureIgnoreCase))
                        {
                            serviceName = strArray[i].Substring(2);
                            list.Add(new Station(serviceName, strArray[++i]));
                        }
                        serviceName = string.Empty;
                    }
                }
                else
                {
                    for (int j = 0; j < strArray.Length; j++)
                    {
                        list.Add(new Station(string.Empty, strArray[j].Trim()));
                    }
                }
                return list.ToArray();
            }
        }
    }
}